package com.fasteat.search.util;

import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 填充排序Map集合工具类
 */
public class SearchUtil {

	/**
	 * 用于填充排序map
	 *
	 * @param map
	 * @param field
	 * @param value
	 */
	public static void setSortType(Map map, String field, int value) {
		if (value < 1 || value > 2 || StringUtils.isEmpty(field)) {
			return;
		}
		if (value == 1) {
			map.put(field, SortOrder.ASC);
		} else {
			map.put(field, SortOrder.DESC);
		}
	}

	/**
	 * 设置高亮
	 *
	 * @param keywords
	 * @param preTags
	 * @param postTags
	 * @return
	 */
	public static HighlightBuilder highlightBuilder(Collection<String> keywords, String preTags, String postTags) {
		HighlightBuilder highlightBuilder = new HighlightBuilder();
		highlightBuilder.preTags(preTags).postTags(postTags);
		keywords.forEach(keyword -> highlightBuilder.field(keyword));
		return highlightBuilder;
	}
}
